//
//  UserAuthMiddleware.swift
//  
//
//  Created by Jiang Chencheng on 2024/9/9.
//

import Vapor
import Fluent


@available(*, deprecated, message: "不要再使用该中间件了")
struct UserAuthMiddleware: AsyncMiddleware {
    func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response {
        let sessionToken = try request.auth.require(SessionToken.self)
        guard let user = try await User.query(on: request.db)
            .filter(\.$id == sessionToken.userId)
            .first() else {
            throw BizError.userNotFound
        }
        try await request.cache.set(Request.authedUserKey, to: user)
        return try await next.respond(to: request)
    }
}
